/**
 * @Author: 宋计民
 * @Date: 2022-04-26 09:24:06
 * @Version: 0.0.1
 * @Content: 判断数据类型
 */

export function isFunction(data: (value: any) => void): boolean {
  return Object.prototype.toString.call(data) === '[object Function]'
}

/**
 * 判断是否是布尔类型
 * @param {*} data
 * @returns
 */
export function isBoolean(data: boolean): boolean {
  return Object.prototype.toString.call(data) === '[object Boolean]'
}

/**
 * 判断是否是对象类型
 * @param {*} data
 * @returns
 */
export function isObject(data: object): boolean {
  return Object.prototype.toString.call(data) === '[object Object]'
}

/**
 * 判断是否是空对象
 * @param data
 * @return {*|number}
 */
export function isEmptyObject(data: object): boolean {
  return isObject(data) && Object.keys(data).length === 0
}

/**
 *
 * @param {Object | Array} data
 * @returns {boolean}
 */
export function isDefine(data: any): boolean {
  return data !== undefined && data !== null
}

/**
 * 判断是否是promise对象
 * @param data
 * @returns {*|boolean}
 */
export function isPromise(data: Promise<any>): boolean {
  return (
    isDefine(data) &&
    typeof data.then === 'function' &&
    typeof data.catch === 'function'
  )
}

/**
 * 判断是否是JSON数据
 * @returns
 */
export function isJson(data: string): boolean | undefined {
  if (typeof data === 'string') {
    try {
      const obj = JSON.parse(data)
      return !!(typeof obj === 'object' && obj)
    } catch (e) {
      return false
    }
  }
}
